use Test::More;
use Data::Dumper;
use strict;

use_ok('Finance::YahooStreamer');

my $streamer = new Finance::YahooStreamer(
    symbols => [qw(YHOO GOOG FAZ)],
    debug   => 1,
);
ok( $streamer, 'new' );

$streamer->{'sub_recv'} = sub {
  my $symbol = shift;
  my $obj = shift;
  warn $symbol.':'.Dumper($obj);
  if (exists($obj->{'z08'})) { # after hour
    my $change = $obj->{'z08'};
    my $percent = $obj->{'z09'};
    ok($change > 0, "$symbol: after hour: up") if $percent > 0;
    ok($change < 0, "$symbol: after hour: down") if $percent < 0;
    ok($change == 0, "$symbol: after hour: unchange") if $percent == 0;
  } elsif (exists($obj->{'l10'})) {
    my $high = $obj->{'h00'};
    my $low = $obj->{'g00'};
    my $last = $obj->{'l10'};
    my $change = $obj->{'c10'};
    my $percent = $obj->{'p20'};
    ok($high, "$symbol: delayed: high");
    ok($low, "$symbol: delayed: low");
    ok($last, "$symbol: delayed: last");
    ok($high >= $low, "$symbol: delayed: high:$high >= low:$low");
    ok($high >= $last, "$symbol: delayed: high:$high >= last:$last");
    ok($last >= $low, "$symbol: delayed: last:$last >= low:$low");
    ok($change > 0, "$symbol: delayed: up") if $percent > 0;
    ok($change < 0, "$symbol: delayed: down") if $percent < 0;
    ok($change == 0, "$symbol: delayed: unchange") if $percent == 0;
  } else {
    ok(0, 'unknown');
  }
};

$streamer->{'sub_hrtbt'} = sub {
    my $obj = shift;
    warn Dumper($obj);
    my $time = $obj->{'unixtime'};
    ok($time, 'unixtime');
};

$streamer->receive;
ok($streamer, 'receive');

done_testing();
